package com.autumn.file.storage.configure;

import java.io.Serializable;

import org.springframework.boot.context.properties.ConfigurationProperties;

import com.aliyun.oss.model.CannedAccessControlList;
import com.autumn.util.StringUtils;

/**
 * 存储属性
 * 
 * @author 老码农 2019-03-11 00:39:13
 */
@ConfigurationProperties(prefix = AutumnStorageProperties.PREFIX)
public class AutumnStorageProperties implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7010871624244378881L;

	/**
	 * 属性前缀
	 */
	public final static String PREFIX = "autumn.storage.client";

	private AliyunStorageClientProperties aliyun;
	private LocalStorageClientProperties local;

	/**
	 * 
	 */
	public AutumnStorageProperties() {
		this.aliyun = new AliyunStorageClientProperties();
		this.local = new LocalStorageClientProperties();
	}

	/**
	 * 获取
	 * 
	 * @return
	 */
	public AliyunStorageClientProperties getAliyun() {
		return aliyun;
	}

	public void setAliyun(AliyunStorageClientProperties aliyun) {
		this.aliyun = aliyun;
	}

	public LocalStorageClientProperties getLocal() {
		return local;
	}

	public void setLocal(LocalStorageClientProperties local) {
		this.local = local;
	}

	/**
	 * 阿里云存储属性
	 * 
	 * @author 老码农 2019-03-13 20:27:52
	 */
	public class AliyunStorageClientProperties extends AbstractStorageClientProperties {

		/**
		 * 
		 */
		private static final long serialVersionUID = 7609832694863934520L;

		/**
		 * bean条件属性
		 */
		public static final String BEAN_CONDITIONAL_PROPERTY = AutumnStorageProperties.PREFIX + ".aliyun.enable";

		private String accessKeyId;
		private String accessKeySecret;
		private String cannedACL = "public-read"; // default、private、public-read、public-read-write

		public String getAccessKeyId() {
			return accessKeyId;
		}

		public void setAccessKeyId(String accessKeyId) {
			this.accessKeyId = accessKeyId;
		}

		public String getAccessKeySecret() {
			return accessKeySecret;
		}

		public void setAccessKeySecret(String accessKeySecret) {
			this.accessKeySecret = accessKeySecret;
		}

		/**
		 * 获取acl权限
		 * 
		 * @return default、private、public-read、public-read-write
		 */
		public String getCannedACL() {
			return cannedACL;
		}

		/**
		 * acl权限
		 * 
		 * @param cannedACL
		 *            default、private、public-read、public-read-write
		 */
		public void setCannedACL(String cannedACL) {
			this.cannedACL = cannedACL;
		}

		/**
		 * 
		 * @return
		 */
		public CannedAccessControlList toCannedAccessControlList() {
			if (StringUtils.isNullOrBlank(this.getCannedACL())) {
				return CannedAccessControlList.PublicRead;
			}
			try {
				return CannedAccessControlList.parse(this.getCannedACL());
			} catch (Exception e) {
				return CannedAccessControlList.PublicRead;
			}
		}
	}

	/**
	 * 本地存储属性
	 * 
	 * @author 老码农 2019-03-13 20:27:52
	 */
	public class LocalStorageClientProperties extends AbstractStorageClientProperties {

		/**
		 * 
		 */
		private static final long serialVersionUID = -6712168217917587187L;

		/**
		 * bean条件属性
		 */
		public static final String BEAN_CONDITIONAL_PROPERTY = AutumnStorageProperties.PREFIX + ".local.enable";

		/**
		 * 默认终节点
		 */
		public static final String DEFAULT_ENDPOINT = "/";

		/**
		 * 默认分区
		 */
		public static final String DEFAULT_BUCKET_NAME = "files";

		private String rootFilePath = "";

		public LocalStorageClientProperties() {
			this.setEndpoint(DEFAULT_ENDPOINT);
			this.setRootFilePath("");
			this.setDefaultBucketName(DEFAULT_BUCKET_NAME);
		}

		/**
		 * 获取根文件路径
		 * 
		 * @return
		 */
		public String getRootFilePath() {
			return rootFilePath;
		}

		/**
		 * 设置根文件路径
		 * 
		 * @param rootFilePath
		 *            根文件路径
		 */
		public void setRootFilePath(String rootFilePath) {
			this.rootFilePath = rootFilePath;
		}

	}

	/**
	 * 存储客户端抽象属性
	 * 
	 * @author 老码农 2019-03-13 20:27:52
	 */
	public abstract class AbstractStorageClientProperties implements Serializable {

		/**
		 * 
		 */
		private static final long serialVersionUID = 3665675392132217354L;

		private boolean enable = false;
		private String endpoint;
		private String defaultBucketName;
		private int readBlockSize = 2048;
		private int writeBlockSize = 2048;

		/**
		 * 获取是否启用
		 * 
		 * @return
		 */
		public boolean isEnable() {
			return enable;
		}

		/**
		 * 设置是否启用
		 * 
		 * @param enable
		 *            是否启用
		 */
		public void setEnable(boolean enable) {
			this.enable = enable;
		}

		/**
		 * 获取终节点
		 * 
		 * @return
		 */
		public String getEndpoint() {
			return endpoint;
		}

		/**
		 * 设置终节点
		 * 
		 * @param endpoint
		 *            终节点
		 */
		public void setEndpoint(String endpoint) {
			this.endpoint = endpoint;
		}

		/**
		 * 获取默认分区
		 * 
		 * @return
		 */
		public String getDefaultBucketName() {
			return defaultBucketName;
		}

		/**
		 * 设置默认分区
		 * 
		 * @param defaultBucketName
		 *            默认分区
		 */
		public void setDefaultBucketName(String defaultBucketName) {
			this.defaultBucketName = defaultBucketName;
		}

		/**
		 * 获取读数据块大小
		 * 
		 * @return
		 */
		public int getReadBlockSize() {
			return readBlockSize;
		}

		/**
		 * 设置读数据块大小
		 * 
		 * @param readBlockSize
		 *            读数据块大小
		 */
		public void setReadBlockSize(int readBlockSize) {
			this.readBlockSize = readBlockSize;
		}

		/**
		 * 获取写数据块大小
		 * 
		 * @return
		 */
		public int getWriteBlockSize() {
			return writeBlockSize;
		}

		/**
		 * 设置写数据块大小
		 * 
		 * @param writeBlockSize
		 *            写数据块大小
		 */
		public void setWriteBlockSize(int writeBlockSize) {
			this.writeBlockSize = writeBlockSize;
		}

	}

}
